// pages/post/post.js
var app = getApp();
var myDate = new Date();
var that;
//格式化日期
function formate_data(myDate) {
  let month_add = myDate.getMonth() + 1;
  var formate_result = myDate.getFullYear() + '-'
    + month_add + '-'
    + myDate.getDate()
  return formate_result;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noteMaxLen: 200,//备注最多字数
    title:"",
    contactValue:"",
    content: "",
    rcontent: "",
    noteNowLen: 0,//备注简介当前字数
    rnoteNowLen: 0,//备注要求当前字数
    date: formate_data(myDate),
    accounts: ["微信号", "QQ号", "手机号"],
    accountIndex: 0,
    showTopTips: false,
    TopTips: '',
    userid: null,
    isAgree: false,
    tabBar: {
      "color": "#878787",
      "selectedColor": "#000",
      "borderStyle": "black",
      "backgroundColor": "#fcfcfc",
      "list": [
        {
          "pagePath": "../team_index/index",
          "iconPath": "../../static/imgs/tabBar/index.png",
          "selectedIconPath": "../../static/imgs/tabBar/index-on.png",
          "text": "首页"
        },
        {
          "pagePath": "../team_post/post",
          "iconPath": "../../static/imgs/tabBar/add.png",
          "selectedIconPath": "../../static/imgs/tabBar/add-on.png",
          "text": "发布",
          active: true
        },
        {
          "pagePath": "../team_self/self",
          "iconPath": "../../static/imgs/tabBar/account.png",
          "selectedIconPath": "../../static/imgs/tabBar/account-on.png",
          "text": "我的"
        }
      ],
      "position": "bottom"
    }
  },

  //简介字数改变触发事件
  rbindTextAreaChange: function (e) {
    var that = this
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > that.data.noteMaxLen)
      return;
    that.setData({
      rcontent: value, rnoteNowLen: len
    })
  },

  //要求字数改变触发事件
  bindTextAreaChange: function (e) {
    var that = this
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > that.data.noteMaxLen)
      return;
    that.setData({
      content: value, noteNowLen: len
    })
  },
  //改变时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  //改变联系方式
  bindAccountChange: function (e) {
    this.setData({
      accountIndex: e.detail.value
    })
  },
  //用户须知展示和关闭
  tapNotice: function (e) {
    if (e.target.id == 'notice') {
      this.hideNotice();
    }
  },
  showNotice: function (e) {
    this.setData({
      'notice_status': true
    });
  },
  hideNotice: function (e) {
    this.setData({
      'notice_status': false
    });
  },
  //同意相关条例
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  //表单验证
  showTopTips: function () {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  //提交表单
  submitForm: function (e) {
    // 获取表单信息
    var userid = wx.getStorageSync('userid');
    var title = e.detail.value.title;
    var starttime = formate_data(myDate);
    var endtime = this.data.date;
    var content = e.detail.value.content;
    var rcontent = e.detail.value.rcontent;
    var contactindex = this.data.accountIndex;
    if (contactindex == 0) {
      var contactWay = "微信号";
    } else if (contactindex == 1) {
      var contactWay = "QQ号";
    } else if (contactindex == 2) {
      var contactWay = "手机号";
    }
    var contactValue = e.detail.value.contactValue;
    var wxReg = new RegExp("^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$");
    var qqReg = new RegExp("[1-9][0-9]{4,}");
    var phReg = /^1[34578]\d{9}$/;
    if (that.data.isAgree == false) {
      wx.showModal({
        title: '提示',
        content: '请先阅读并同意《发起须知》'
      })
      return;
    }
    //表单信息验证
    if (title == "") {
      this.setData({
        showTopTips: true,
        TopTips: '请输入队名'
      });
    } else if (content == "") {
      this.setData({
        showTopTips: true,
        TopTips: '请输入简介内容'
      });
    } else if (rcontent == "") {
      this.setData({
        showTopTips: true,
        TopTips: '请输入组队要求'
      });
    } else if (contactValue == "") {
      this.setData({
        showTopTips: true,
        TopTips: '请输入联系方式'
      });
    } else if (contactWay == "微信号" && !wxReg.test(contactValue)) {
      this.setData({
        showTopTips: true,
        TopTips: '微信号格式不正确'
      });
    } else if (contactWay == "手机号" && !phReg.test(contactValue)) {
      this.setData({
        showTopTips: true,
        TopTips: '手机号格式不正确'
      });
    } else if (contactWay == "QQ号" && !qqReg.test(contactValue)) {
      this.setData({
        showTopTips: true,
        TopTips: 'QQ号格式不正确'
      });
    } else {
      console.log('校验完毕');
      that.setData({
        isLoading: true,
        isdisabled: true
      })
      console.log(starttime)
      var id = "";
      var auth = getApp().globalData.kind;
      if (auth == "1") {
        id = getApp().globalData.user.studentNo
      } else if (auth == "2") {
        id = getApp().globalData.user.teacherNo
      } else if (auth == "3") {
        id = getApp().globalData.user.adminNo
      } else if (auth == "4") {
        id = getApp().globalData.user.donatorNo
      };
      console.log(id);
      wx.request({
        url: getApp().globalData.my_server +'/team/release',
        data: {
          id: id,
          role: getApp().globalData.kind,
          title: title,
          starttime: starttime,
          endtime: endtime,
          contactWay: contactWay,
          contactValue: contactValue,
          content: content,
          rcontent: rcontent,
        },
        method: 'POST',
        headr: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res)
          console.log(starttime)
          that.setData({
            title: "",
            contactValue: "",
            content: "",
            rcontent: "",
            noteNowLen: 0,//备注简介当前字数
            rnoteNowLen: 0,//备注要求当前字数
            date: formate_data(myDate),
            accountIndex: 0,
            showTopTips: true,
            TopTips: '发布成功',
            isLoading: false,
            isdisabled: false
          })
        },
      })
    }
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.setData({//初始化数据
      src: "",
      isSrc: false,
      ishide: "0",
      autoFocus: true,
      isLoading: false,
      loading: true,
      isdisabled: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})